<template>
  <div>
    <el-button v-if="visible" style="margin-left:12px" type="primary" size="small" @click="approval(form)">审 核</el-button>
  </div>
</template>

<script>
import {formDataSave, formDataShow} from './approval'

export default {
  name: 'ApprovalButton',
  props: {
    taskId: {
      required: true,
      type: String
    },
    visible: {
      required: false,
      type: Boolean,
      default: true
    },
    form: {
      required: true,
      type: Object
    },
    confirmValidate: {
      required: false,
      type: Function,
      default: function () {
        return true;
      }
    },
    approvalCallback: {
      required: false,
      type: Function,
      default: function () {
      }
    }
  },
  data() {
    return {
      formData: [],
      opinionForm: {
        inputOpinion: null
      },
    }
  },
  mounted() {

  },
  methods: {
    approval() {
      //流程渲染
      this.formData = [];
      formDataShow(this.taskId).then(response => {
        let datas = response.data
        for (let i = 0; i < datas.length; i++) {
          let strings = datas[i].split('--__!!')
          let controlValue = null
          let controlDefault = null
          switch (strings[1]) {
            case 'radio':
              controlValue = 0
              controlDefault = strings[4]
              break
          }
          this.formData.push({
            controlId: strings[0],
            controlType: strings[1],
            controlLable: strings[2],
            controlIsParam: strings[3],
            controlValue: controlValue,
            controlDefault: controlDefault
          })
        }
        this.open = true
      })
      const _this = this
      const h = this.$createElement
      this.$msgbox({
        title: '审核',
        message: h('el-form', {
          ref: 'opinionForm',
          props: {
            model: _this.opinionForm
          }
        }, [
          h('el-form-item', {
            attrs: {
              prop: 'inputOpinion',
            },
            props: {
              rules: {
                required: true,
                message: '审批意见不能为空'
              }
            }
          }, [
            h('textarea', {
              'class': [
                'el-textarea__inner'
              ],
              attrs: {
                placeholder: '请录入审批意见',
              },
              props: {
                value: _this.opinionForm.inputOpinion,
              },
              style: {
                height: '70px'
              },
              on: {
                input: function (event) {
                  _this.opinionForm.inputOpinion = event.target.value
                }
              }
            }, '')
          ]),
        ]),
        distinguishCancelAndClose: true,
        showCancelButton: true,
        confirmButtonText: '同意',
        cancelButtonText: '驳回',
        inputType: 'textarea',
        beforeClose: async function (action, instance, done) {
          //同意
          if (action === 'confirm') {
            if (_this.confirmValidate()) {
              //获取审批意见
              _this.formData[1]['controlValue'] = _this.opinionForm.inputOpinion
              await formDataSave(_this.taskId, _this.formData).then(response => {
                if (response.code === 200) {
                  this.$message({
                    type: 'success',
                    message: '审核通过！'
                  })
                }
              })
              done()
            }
            //驳回
          } else if (action === 'cancel') {
            //修改审批状态
            _this.formData[0]['controlValue'] = 1
            //获取审批意见
            _this.formData[1]['controlValue'] = _this.opinionForm.inputOpinion
            _this.$refs['opinionForm'].validate(async (valid) => {
              if (valid) {
                await formDataSave(_this.taskId, _this.formData).then(response => {
                  if (response.code === 200) {
                    this.$message({
                      type: 'warning',
                      message: '已驳回！'
                    })
                  }
                })
                done()
              }
            })
            //关闭审批页面
          } else if (action === 'close') {
            this.$message({
              type: 'info',
              message: '您已取消审核'
            })
            done()
          }
        },
        callback: function (action, instance) {
          if (action !== 'close') {
            _this.approvalCallback();
          }
          _this.$refs['opinionForm'].resetFields()
        }
      }).then(value => {
      }).catch((action) => {
      })
    },
  }
}
</script>

<style scoped>

</style>
